﻿// https://leetcode-cn.com/problems/maximum-binary-tree/
// 654. 最大二叉树

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <limits.h>

using namespace std;

// struct ListNode {
//     int val;
//     ListNode *next;
//     ListNode() : val(0), next(nullptr) {}
//     ListNode(int x) : val(x), next(nullptr) {}
//     ListNode(int x, ListNode *next) : val(x), next(next) {}
// };

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        TreeNode *root = new TreeNode(0);
        if (nums.size() == 1) {
            root->val = nums[0];
            return root;
        }

        int max_val = 0;
        int max_val_index = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] > max_val) {
                max_val = nums[i];
                max_val_index = i;
            }
        }
        root->val = max_val;

        if (max_val_index > 0) {
            vector<int> new_vec(nums.begin(), nums.begin() + max_val_index);
            root->left = constructMaximumBinaryTree(new_vec);
        }
        if (max_val_index < nums.size() - 1) {
            vector<int> new_vec(nums.begin() + max_val_index + 1, nums.end());
            root->right = constructMaximumBinaryTree(new_vec);
        }

        return root;
    }
};

int main() {
    Solution obj = Solution();
}